/*
 * FileName：VoteApplyServiceImpl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.service.impl;

import javax.annotation.Resource;

import com.smart.common.util.AjaxResult;
import com.smart.common.util.StringUtil;
import com.smart.common.util.Util;
import com.smart.wx.wxcms.dao.VoteApplyMapper;
import com.smart.wx.wxcms.dao.VoteMapper;
import com.smart.wx.wxcms.dao.VoteRecordMapper;
import com.smart.wx.wxcms.entity.Vote;
import com.smart.wx.wxcms.entity.VoteApply;
import com.smart.wx.wxcms.entity.VoteRecord;
import com.smart.wx.wxcms.service.VoteApplyService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 投票报名表
 *
 * @author hermit
 * @version 2.0
 * @date 2018-07-11 20:58:45
 */
@Service
public class VoteApplyServiceImpl implements VoteApplyService {

	@Resource
	private VoteMapper voteMapper;

	@Resource
	private VoteApplyMapper voteApplyMapper;

	@Resource
	private VoteRecordMapper voteRecordMapper;

    @Override
    public VoteApply queryObject(VoteApply voteApply) throws Exception {
        return voteApplyMapper.queryObject(voteApply);
    }
	
	@Override
	public List<VoteApply> queryList(VoteApply voteApply) throws Exception {
		return voteApplyMapper.queryList(voteApply);
	}

    @Override
    public List<VoteApply> queryListByPage(VoteApply voteApply) throws Exception {
        return voteApplyMapper.queryListByPage(voteApply);
    }

	@Override
	public Integer queryCount(VoteApply voteApply) throws Exception {
        Integer ct = voteApplyMapper.queryCount(voteApply);
        return ct == null ? 0 : ct;
	}
	
	@Override
	public Integer insert(VoteApply voteApply) throws Exception {
        return voteApplyMapper.insert(voteApply);
	}
	
	@Override
	public Integer update(VoteApply voteApply) throws Exception {
        return voteApplyMapper.update(voteApply);
	}

	@Override
	public Integer delete(VoteApply voteApply) throws Exception {
		String voteApplyId = voteApply.getId();
		if (StringUtil.isNotBlank(voteApplyId)) {
			this.voteApplyMapper.deleteBatch(Arrays.asList(voteApplyId.split(",")));
		}
		return 1;

	}

	@Override
	public Integer deleteBatch(List<String> ids) throws Exception {
		return voteApplyMapper.deleteBatch(ids);
	}

	@Override
	public List<VoteApply> getVoteApplyListByPage(VoteApply voteApply) throws Exception{
		List<VoteApply> list = this.voteApplyMapper.queryListByPage(voteApply);

		if(list != null && list.size() > 0){
			for(VoteApply temp : list){
				Vote vote = new Vote();
				vote.setId(temp.getVoteId());
				vote = this.voteMapper.queryObject(vote);

				String voteApplyId = temp.getId();

				VoteRecord voteRecord = new VoteRecord();
				voteRecord.setVoteApplyId(voteApplyId);
				int count = this.voteRecordMapper.queryCount(voteRecord);
				temp.setTotalCount(count + "");
				temp.setVoteName(vote.getName());
			}
		}
		return list;
	}


	@Override
	public AjaxResult save(VoteApply voteApply) throws Exception {
    	String openId = voteApply.getOpenId();

		/******* STEP_1：如果已经报名过 则禁止再次报名 ***********/
		VoteApply tempBean = new VoteApply();
		tempBean.setOpenId(openId);
		tempBean.setVoteId(voteApply.getVoteId());
		int count = this.voteApplyMapper.queryCount(tempBean);
		if(count > 0){

			return AjaxResult.failure("您已经报名!");
		}

		/******* STEP_2：新增 *********************************/

		Vote vote = new Vote();
		vote.setId(voteApply.getVoteId());
		vote = this.voteMapper.queryObject(vote);

		int max = voteApplyMapper.getMaxNumber(voteApply);

		voteApply.setId(Util.getUUID());
		voteApply.setCreateTime(new Date());
		voteApply.setOpenId(openId);
		voteApply.setAccount(vote.getAccount());
		voteApply.setNumber(max + 1);
		voteApplyMapper.insert(voteApply);

		return AjaxResult.saveSuccess();
	}
}
